<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>
<span id="show" style="display:none">123</span>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <!--    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>-->
        <!--    <button class="layui-btn layui-btn-sm" lay-event="isAll" id="all">验证是否全选</button>-->
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-mini layui-btn-normal" lay-event="update">更新缓存</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-mini layui-btn-normal" lay-event="edit">修改信息</a>
</script>


<script src="/static/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table', 'laypage','layer', 'jquery', 'form', 'laydate'], function () {
        var table = layui.table;
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;

        table.render({

            elem: '#test',
            url: '/information/listAll',
            toolbar: '#toolbarDemo',
            title: '用户数据表',
            totalRow: true,
            cols: [
                [{
                    type: 'checkbox',
                    fixed: 'left'
                }, {
                    field: 'id',
                    title: 'ID',
                    width: 75,
                    // fixed: 'left',
                    unresize: true,
                    sort: true,

                }, {
                    field: 'title',
                    title: '活动标题',
                    width: 250,
                    edit: 'text'
                }, {
                    field: 'content',
                    title: '活动内容',
                    width: 500,
                    sort: true,
                    // totalRow: true
                }, {
                    field: 'releaseTime',
                    title: '发布时间',
                    width: 200,
                    edit: 'text',
                    sort: true
                }, {
                    field: 'authors',
                    title: '作者',
                    width: 100
                }, {
                    field: 'categoryName',
                    title: '类别',
                    width: 100
                }, {
                    // fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 300
                }]
            ],
            page: false
        });

        var arr = [];
        table.on('checkbox(test)', function (obj) {
            var flg = 0;
            if (obj.type != 'all') {
                if (arr.length == 0) {
                    if (obj.checked == true) {
                        arr.push(obj);

                    }
                } else {
                    $.each(arr, function (index, value) {
                        if (obj.data.id == value.data.id) {
                            flg = 1;
                            if (obj.checked == false) {
                                arr.splice(index, 1);
                            }
                        } else {
                            flg = 0;
                        }

                    });
                    if (flg == 0) {
                        arr.push(obj);
                    }
                }
            }
        });

        //工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);

            // 					console.log(table.checkStatus(obj.config.id));
            // console.log(obj.config.elem)

            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    $(this).parent().parent().parent().parent().toggle();
                    $("#show").toggle();
                    $("#all").toggle();
                    console.log(data)
                    $('#show').html(JSON.stringify(data))
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    console.log(checkStatus)
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选')
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'update') {
                layer.confirm('确定更新缓存？', function (index) {
                    layer.close(index);
                    console.log(data.id);
                    $.ajax({
                        url: '/information/updateCacheInfo',
                        type: 'POST',
                        data: "id=" + data.id,
                        success: function (res) {
                            var returnCode = res.code;//取得返回数据（Sting类型的字符串）的信息进行取值判断
                            console.log(res.code);
                            console.log(res);
                            if (returnCode == 0) {
                                layer.confirm('更新成功！', function (index) {
                                    layer.close(index);
                                })
                            } else {
                                layer.confirm('更新失败！', function (index) {
                                    layer.close(index);
                                })
                            }
                        }
                    })
                    return false;
                });
            }
            else if (obj.event === 'edit') {
                console.log(obj.data);
            }

        });




    });
</script>
</body>
</html>
